/* 
 * File:   VistaNivel.cpp
 * Author: agu
 * 
 * Created on 7 de junio de 2014, 1:12
 */

#include "VistaNivel.h"
#include <iostream>

VistaNivel::VistaNivel() {
    this->builder = Gtk::Builder::create();
    try {
        this->builder->add_from_file("nivel.glade");
    } catch (const Glib::FileError& ex) {
        std::cout << "FileError: " << ex.what() << std::endl;

    } catch (const Glib::MarkupError& ex) {
        std::cout << "MarkupError: " << ex.what() << std::endl;

    } catch (const Gtk::BuilderError& ex) {
        std::cout << "BuilderError: " << ex.what() << std::endl;

    }
    this->loadWidgets();
    this->connectWidgets();
}

void VistaNivel::loadWidgets() {
    this->builder->get_widget("window", this->window);
    this->builder->get_widget("entryName", this->entryName);
    this->builder->get_widget("buttonContinue", this->buttonContinue);
    this->builder->get_widget("buttonBack", this->buttonBack);
    this->builder->get_widget("buttonGenerate", this->buttonGenerate);
    this->builder->get_widget("buttonZombieRed", this->buttonZombieRed);
    this->builder->get_widget("buttonZombieBlue", this->buttonZombieBlue);
    this->builder->get_widget("buttonZombieGreen", this->buttonZombieGreen);
    this->builder->get_widget("buttonFriend", this->buttonFriend);
    this->builder->get_widget("buttonGround", this->buttonGround);
    this->builder->get_widget("buttonBox", this->buttonBox);
    this->builder->get_widget("buttonMetal", this->buttonMetal);
    this->builder->get_widget("buttonBoxEnemy", this->buttonBoxEnemy);
    this->builder->get_widget("spinWidth", this->spinWidth);
    this->builder->get_widget("spinHeight", this->spinHeight);
    this->builder->get_widget("grid", this->grid);

    listTargets.push_back(Gtk::TargetEntry("STRING"));
    listTargets.push_back(Gtk::TargetEntry("text/plain"));
    buttonGround->drag_source_set(listTargets);
    buttonGround->signal_drag_data_get().connect(sigc::mem_fun(*this,
            &VistaNivel::on_button_drag_data_get));
    buttonMetal->drag_source_set(listTargets);
    buttonMetal->signal_drag_data_get().connect(sigc::mem_fun(*this,
            &VistaNivel::on_button_drag_data_get));
    //    this->builder->get_widget("scrollVertical", this->scrollVertical);
    //    this->builder->get_widget("scrollHorizontal", this->scrollHorizontal);

}

void VistaNivel::connectWidgets() {
    this->window->signal_hide().connect(sigc::mem_fun(this, &VistaNivel::close));
    this->buttonBack->signal_clicked().connect(sigc::mem_fun(this, &VistaNivel::back));
    this->buttonGenerate->signal_clicked().connect(sigc::mem_fun(this, &VistaNivel::generateGridMap));
    this->spinWidth->signal_value_changed().
            connect(sigc::mem_fun(this, &VistaNivel::incrementWidth));
    this->spinHeight->signal_value_changed().
            connect(sigc::mem_fun(this, &VistaNivel::incrementHeight));
}

VistaNivel::~VistaNivel() {
    delete this->window;
}

void VistaNivel::run() {
    this->window->show();
}

void VistaNivel::close() {
    this->window->close();
}

void VistaNivel::back() {
    this->window->set_visible(false);
    this->windowsBack->set_visible(true);
}

void VistaNivel::next() {

}

void VistaNivel::setWindowsBack(Gtk::Window *windowBack) {
    this->windowsBack = windowBack;
}

unsigned int VistaNivel::getId() {
    return this->level.getId();
}

void VistaNivel::incrementWidth() {
    this->level.incrementMapWidth();
}

void VistaNivel::incrementHeight() {
    this->level.incrementMapHeight();
}

void VistaNivel::generateGridMap() {
    if (this->level.checkDimension()) {
        //Por cada fila
        for (int i = 0; i < this->level.getMapHeight(); i++) {
            //Recorro todas las columnas
            for (int j = 0; j < this->level.getMapWidth(); j++) {
                Gtk::Image* img = Gtk::manage(new Gtk::Image);
                img->set("tierra.png"); //Emprolijar                
                img->drag_dest_set(listTargets);
                img->signal_drag_data_received().connect(sigc::mem_fun(*this,
                        &VistaNivel::on_label_drop_drag_data_received));
                this->grid->attach(*img, i, j, 1, 1);
            }
        }
        this->window->show_all();
    } else {
        //Lanzar mensaje de que el tamaño no es el adecuado
    }
}

void VistaNivel::on_button_drag_data_get(
        const Glib::RefPtr<Gdk::DragContext>& context,
        Gtk::SelectionData& selection_data, guint info, guint time) {
    std::cout << "Info: " << info<< std::endl;
    std::cout << "Time: " << time<< std::endl;
    Gtk::Widget* widget = Gtk::Widget::drag_get_source_widget(context);
    //widget->
    std::cout << "Name: " <<  widget->get_path().to_string()<< std::endl;
    selection_data.set(selection_data.get_target(), 8 /* 8 bits format */,
            (const guchar*) "I'm Data!",
            9 /* the length of I'm Data! in bytes */);
}

void VistaNivel::on_label_drop_drag_data_received(
        const Glib::RefPtr<Gdk::DragContext>& context, int x, int y,
        const Gtk::SelectionData& selection_data, guint info, guint time) {

    const int length = selection_data.get_length();
    if ((length >= 0) && (selection_data.get_format() == 8)) {
        std::cout << selection_data.get_data() << std::endl;
        std::cout << selection_data.get_format() << std::endl;
        std::cout << selection_data.get_text() << std::endl;
        std::cout << selection_data.get_target() << std::endl;
        std::cout << context->get_selection() << std::endl;
        std::cout << context->list_targets().at(0) << std::endl;
        
        std::cout << "Received \"" << selection_data.get_data_as_string()
                << "\" in label " << std::endl;
    }

    context->drag_finish(false, false, time);
}